package com.myphoto.base.log.serviceimpl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myphoto.base.log.service.LogService;
import com.myphoto.base.log.vo.P6spyLogVO;
import com.myphoto.base.log.vo.SysLogVO;
import com.myphoto.base.log.vo.UrlLogVO;
import com.myphoto.base.mybatis.log.dao.TbLogP6spyDao;
import com.myphoto.base.mybatis.log.dao.TbLogSysDao;
import com.myphoto.base.mybatis.log.dao.TbLogUrlDao;
import com.myphoto.base.mybatis.log.entity.TbLogP6spyEntity;
import com.myphoto.base.mybatis.log.entity.TbLogSysEntity;
import com.myphoto.base.mybatis.log.entity.TbLogUrlEntity;
import com.myphoto.base.util.UserFun;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Transactional(rollbackFor = Exception.class)
public class LogServiceImpl implements LogService {

    @Value(value = "${spring.application.name}")
    private String xmmc;
    @Resource
    private TbLogSysDao tbLogSysDao;
    @Resource
    private TbLogP6spyDao tbLogP6spyDao;
    @Resource
    private TbLogUrlDao tbLogUrlDao;

    @Override
    public void saveSysLog(TbLogSysEntity sysLogEntity){
        try {
            tbLogSysDao.insert(sysLogEntity);
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void writeDB(String message) throws Exception{
        String[] msgSz = null;
        /** 检查信息合法性 **/
        boolean flag = false;
        if(message!=null && message.trim().length()>0){
            msgSz = message.split("&");
            //信息拆分后，长度为3
            if(msgSz.length==3){
                flag = true;
            }
        }
        //性能监控信息格式不符
        if(flag==false){
            return;
        }
        int xhsj = new Integer(msgSz[0]);//消耗时间
        String ybySql = msgSz[1];//预编译SQL
        String paramSql = msgSz[2];//带参数SQL
        TbLogP6spyEntity entity = new TbLogP6spyEntity();
        entity.setId(UserFun.getUUID32());
        entity.setLogTime(new Date());
        entity.setXmmc(xmmc);
        entity.setParamSql(ybySql);
        entity.setYbySql(paramSql);
        entity.setXhsj(xhsj);
        tbLogP6spyDao.insert(entity);
    }

    @Override
    public void saveUrlMonitor(TbLogUrlEntity logUrlEntity) throws Exception {
        tbLogUrlDao.insert(logUrlEntity);
    }

    @Override
    public IPage<TbLogSysEntity> loadSysLogList(Page<TbLogSysEntity> page, SysLogVO sysLogVO) throws Exception {
        LambdaQueryWrapper<TbLogSysEntity> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        userLambdaQueryWrapper.like(sysLogVO.getContent()!=null && sysLogVO.getContent().trim().length()>0,TbLogSysEntity::getContent , sysLogVO.getContent());
        userLambdaQueryWrapper.eq(sysLogVO.getLogType()!=null && sysLogVO.getLogType().trim().length()>0,TbLogSysEntity::getLogType , sysLogVO.getLogType());
        userLambdaQueryWrapper.eq(sysLogVO.getModule()!=null && sysLogVO.getModule().trim().length()>0,TbLogSysEntity::getModule , sysLogVO.getModule());
        userLambdaQueryWrapper.like(sysLogVO.getOperator()!=null && sysLogVO.getOperator().trim().length()>0,TbLogSysEntity::getOperator , sysLogVO.getOperator());
        userLambdaQueryWrapper.eq(sysLogVO.getIp()!=null && sysLogVO.getIp().trim().length()>0,TbLogSysEntity::getIp , sysLogVO.getIp());
        if(sysLogVO.getLogTime()!=null){
            Date logtimeAddOne = DateUtil.offsetDay(sysLogVO.getLogTime(),1).toJdkDate();
            userLambdaQueryWrapper.between(TbLogSysEntity::getLogTime ,sysLogVO.getLogTime(),logtimeAddOne);
        }
        userLambdaQueryWrapper.orderByDesc(TbLogSysEntity::getLogTime);
        return tbLogSysDao.selectPage(page , userLambdaQueryWrapper);
    }

    @Override
    public IPage<TbLogP6spyEntity> loadP6spyLogList(Page<TbLogP6spyEntity> page, P6spyLogVO p6spyLogVO) throws Exception {
        LambdaQueryWrapper<TbLogP6spyEntity> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        userLambdaQueryWrapper.like(p6spyLogVO.getYbySql()!=null && p6spyLogVO.getYbySql().trim().length()>0,TbLogP6spyEntity::getYbySql , p6spyLogVO.getYbySql());
        userLambdaQueryWrapper.like(p6spyLogVO.getParamSql()!=null && p6spyLogVO.getParamSql().trim().length()>0,TbLogP6spyEntity::getParamSql , p6spyLogVO.getParamSql());
        userLambdaQueryWrapper.ge(p6spyLogVO.getXhsj()!=null,TbLogP6spyEntity::getXhsj , p6spyLogVO.getXhsj());
        userLambdaQueryWrapper.eq(p6spyLogVO.getXmmc()!=null && p6spyLogVO.getXmmc().trim().length()>0,TbLogP6spyEntity::getXmmc , p6spyLogVO.getXmmc());
        if(p6spyLogVO.getLogTime()!=null){
            Date logtimeAddOne = DateUtil.offsetDay(p6spyLogVO.getLogTime(),1).toJdkDate();
            userLambdaQueryWrapper.between(TbLogP6spyEntity::getLogTime ,p6spyLogVO.getLogTime(),logtimeAddOne);
        }
        userLambdaQueryWrapper.orderByDesc(TbLogP6spyEntity::getLogTime);
        return tbLogP6spyDao.selectPage(page , userLambdaQueryWrapper);
    }

    @Override
    public IPage<TbLogUrlEntity> loadUrlLogList(Page<TbLogUrlEntity> page, UrlLogVO urlLogVO) throws Exception {
        LambdaQueryWrapper<TbLogUrlEntity> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        userLambdaQueryWrapper.like(urlLogVO.getUrlInfo()!=null && urlLogVO.getUrlInfo().trim().length()>0,TbLogUrlEntity::getUrlInfo , urlLogVO.getUrlInfo());
        userLambdaQueryWrapper.ge(urlLogVO.getXhsj()!=null,TbLogUrlEntity::getXhsj , urlLogVO.getXhsj());
        userLambdaQueryWrapper.eq(urlLogVO.getXmmc()!=null && urlLogVO.getXmmc().trim().length()>0,TbLogUrlEntity::getXmmc , urlLogVO.getXmmc());
        userLambdaQueryWrapper.eq(urlLogVO.getUsername()!=null && urlLogVO.getUsername().trim().length()>0,TbLogUrlEntity::getUsername , urlLogVO.getUsername());
        if(urlLogVO.getRecordTime()!=null){
            Date logtimeAddOne = DateUtil.offsetDay(urlLogVO.getRecordTime(),1).toJdkDate();
            userLambdaQueryWrapper.between(TbLogUrlEntity::getRecordTime ,urlLogVO.getRecordTime(),logtimeAddOne);
        }
        userLambdaQueryWrapper.orderByDesc(TbLogUrlEntity::getRecordTime);
        return tbLogUrlDao.selectPage(page , userLambdaQueryWrapper);
    }

    @Override
    public TbLogSysEntity loadSysLogDetail(String id) throws Exception {
        return tbLogSysDao.selectById(id);
    }

    @Override
    public TbLogP6spyEntity loadP6spyLogDetail(String id) throws Exception {
        return tbLogP6spyDao.selectById(id);
    }

    @Override
    public TbLogUrlEntity loadUrlLogDetail(String id) throws Exception {
        return tbLogUrlDao.selectById(id);
    }
}
